package com.pronosticnews.client.admin.view.editPays;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.pronosticnews.client.admin.resources.PronosticnewsAdminMessages;
import com.pronosticnews.client.admin.resources.PronosticnewsAdminResources;
import com.pronosticnews.shared.model.PaysDto;

public class EditPaysViewImpl extends Composite implements EditPaysView {

	interface EditPaysUiBinder extends UiBinder<Widget, EditPaysViewImpl> {
	}

	private Presenter presenter;

	@UiField
	TextBox txtImage;

	@UiField
	Image imgDrapeau;

	@UiField
	FormPanel form;

	@UiField
	FileUpload uplImage;

	@UiField
	TextBox txtNom;

	@UiField
	Label lblTitle;

	@UiField
	HorizontalPanel panNouveau;

	@UiField
	HorizontalPanel panModifier;

	@UiField
	VerticalPanel containerForm;

	@UiField(provided = true)
	PronosticnewsAdminResources res;

	PronosticnewsAdminMessages messages;

	private PaysDto editPays;

	private final Integer logoSize = 100;

	public EditPaysViewImpl() {
		messages = GWT.create(PronosticnewsAdminMessages.class);
		res = GWT.create(PronosticnewsAdminResources.class);
		res.cssFormEdition().ensureInjected();
		initWidget(GWT.<EditPaysUiBinder> create(EditPaysUiBinder.class)
				.createAndBindUi(this));
		lblTitle.addStyleName(res.cssFormEdition().title_creation());
		initForm();
	}

	private void initForm() {
		form.setAction(com.google.gwt.core.client.GWT.getHostPageBaseURL()
				+ "image/");
		form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
			@Override
			public void onSubmitComplete(SubmitCompleteEvent event) {
				try {
					Integer idImage = Integer.parseInt(event.getResults()
							.replace("<pre>", "").replace("</pre>", ""));
					if (idImage != null) {
						editPays.setDrapeau(idImage);
					}
					presenter.submit(editPays);
				} catch (Exception e) {
					Window.alert(e.getMessage());
				}
			}
		});
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void submitComplete() {
		clearFormulaire();
	}

	@Override
	public void setPays(PaysDto existingPays) {
		if (existingPays.getDrapeau() != null) {
			txtImage.setText(existingPays.getDrapeau().toString());
			imgDrapeau.setUrl(com.google.gwt.core.client.GWT
					.getHostPageBaseURL()
					+ "image/?id="
					+ existingPays.getDrapeau() + "&height=" + logoSize);
			imgDrapeau.setVisible(true);
		}
		txtNom.setText(existingPays.getNom());
	}

	private void clearFormulaire() {
		imgDrapeau.setVisible(false);
		txtImage.setText("");
		txtImage.setValue("");
		txtNom.setValue("");
	}

	@Override
	public void setModeModification(boolean mode) {
		panNouveau.setVisible(!mode);
		panModifier.setVisible(mode);
		clearFormulaire();
		if (mode) {
			lblTitle.setText(messages.modifier_pays());
			lblTitle.addStyleName(res.cssFormEdition().title_modif());
			lblTitle.removeStyleName(res.cssFormEdition().title_creation());
		} else {
			lblTitle.setText(messages.enregistrer_pays());
			containerForm.addStyleName(res.cssFormEdition().title_creation());
			containerForm.removeStyleName(res.cssFormEdition().title_modif());
		}
	}

	@UiHandler({ "btEnregistrer", "btModifier" })
	void onClick(ClickEvent event) {
		editPays = new PaysDto();
		editPays.setNom(txtNom.getValue());
		if (!"".equals(uplImage.getFilename())) {
			form.submit();
		} else {
			presenter.submit(editPays);
		}
		setModeModification(false);
	}

	@UiHandler("btAnnuler")
	void doClickAnnuler(ClickEvent event) {
		setModeModification(false);
		presenter.annuleModification();
	}

}
